package com.example.activitysign.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activitysign.entity.Activity;
import com.example.activitysign.entity.ActivityUserJoin;
import com.example.activitysign.mapper.ActivityMapper;
import com.example.activitysign.mapper.ActivityUserJoinMapper;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityUserJoinService;
import com.example.activitysign.validate.ActivityUserJoinCreateValidate;
import com.example.activitysign.validate.ActivityUserJoinSearchValidate;
import com.example.activitysign.validate.ActivityUserJoinUpdateValidate;
import com.example.activitysign.vo.activity.ActivityUserJoinListedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户活动报名实现类
 *
 * @author fei
 */
@Slf4j
@Service
public class ActivityUserJoinServiceImpl extends ServiceImpl<ActivityUserJoinMapper, ActivityUserJoin> implements IActivityUserJoinService {

    @Resource
    private ActivityUserJoinMapper activityUserJoinMapper;
    @Resource
    private ActivityMapper activityMapper;

    /**
     * 用户活动报名列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ActivityUserJoinListedVo>
     * @author fei
     */
    @Override
    public PageResult<ActivityUserJoinListedVo> list(PageValidate pageValidate, ActivityUserJoinSearchValidate searchValidate) {
        Page<ActivityUserJoin> page = new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize());
        IPage<ActivityUserJoinListedVo> list = activityUserJoinMapper.list(page, searchValidate);
        return PageResult.iPageHandle(list.getTotal(), list.getCurrent(), list.getSize(), list.getRecords());
    }

    /**
     * 用户活动报名详情
     *
     * @param id 主键参数
     * @return ActivityUserJoin
     * @author fei
     */
    @Override
    public ActivityUserJoinListedVo detail(Long id) {
        ActivityUserJoin model = activityUserJoinMapper.selectById(id);
        Assert.notNull(model, "数据不存在");
        ActivityUserJoinListedVo joinListedVo = new ActivityUserJoinListedVo();
        joinListedVo.setId(id);
        joinListedVo.setUserId(String.valueOf(model.getCreateUserId()));
        joinListedVo.setActivityId(model.getActivityId());
        Activity activity = activityMapper.selectById(model.getActivityId());
        joinListedVo.setActivityName(activity.getName());
        joinListedVo.setStatus(model.getStatus());
        return joinListedVo;
    }

    /**
     * 用户活动报名新增
     *
     * @param createValidate 参数
     * @author fei
     */
    @Override
    public Boolean add(ActivityUserJoinCreateValidate createValidate) {
        //判断是否超过报名限制
        Activity activity = activityMapper.selectById(createValidate.getActivityId());
        Integer limitType = activity.getLimitType();
        if (limitType == 1) {
            Integer joinNum = activity.getJoinNum();
            Integer limitNum = activity.getLimitNum();
            if (joinNum + 1 > limitNum) {
                return false;
            }
        }
        ActivityUserJoin model = new ActivityUserJoin();
        BeanUtils.copyProperties(createValidate, model);
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        activityUserJoinMapper.insert(model);
        return true;
    }

    /**
     * 用户活动报名编辑
     *
     * @param updateValidate 参数
     * @author fei
     */
    @Override
    public void edit(ActivityUserJoinUpdateValidate updateValidate) {
        ActivityUserJoin model = activityUserJoinMapper.selectById(updateValidate.getId());
        Assert.notNull(model, "数据不存在!");
        BeanUtils.copyProperties(updateValidate, model);
        model.setUpdateTime(new Date());
        activityUserJoinMapper.updateById(model);
    }

    /**
     * 用户活动报名删除
     *
     * @param id 主键ID
     * @author fei
     */
    @Override
    public void del(Long id) {
        int i = activityUserJoinMapper.deleteById(id);
        Assert.isTrue(i > 0, "数据不存在!");
    }

}

